function [theta,dsig]=principal_orientations_coef_sum(des,thresh1)
% function [theta,dsig]=principal_orientations_coef_sum(des,thresh1)
%
%  calculate principal orientations of a flow field by maximizing its sum
%  of projection coefficients on the basis functions \phi_{k,1}. See the
%  paper for more information.
%
%   INPUTS:
%           des - matrix of descriptors, each COLUMN is a descriptor.
%           thresh1- threshold for pruning the principal orientations. 0.7
%           by default.
%
%
%
%
%
% This file is part of the source-code demonstrating the method published 
% in the paper: 
%
% Wei Liu and Eraldo Ribeiro. Scale and Rotation Invariant Detection 
%      of Singular Patterns in Vector Flow Fields. IAPR International 
%      Workshop on Structural Syntactic Pattern Recognition (S-SSPR), 
%      Cesne, Turkey, 2010.
%
% BIBTEX ENTRY:
%
% @inproceedings{WeiRibeiroSSPR2010,
%   author    = {Wei Liu and Eraldo Ribeiro},
%   title     = {Scale and Rotation Invariant Detection of Singular 
%                Patterns in Vector Flow Fields},
%   booktitle = {IAPR International Workshop on Structural Syntactic 
%                Pattern Recognition (S-SSPR)},
%   year      = {2010},
%   pages     = {XXX-XXX}
% }
%
% The source-code can be obtained from 
%        http://www.cs.fit.edu/~eribeiro/flowdetector/
% 
% You are free to use, change, or redistribute this code in any way you
% want for non-commercial purposes. However, it is appreciated if you 
% maintain the name of the original authors.
%
% (c) 2010 by Wei Liu and Eraldo Ribeiro 
% eribeiro@cs.fit.edu
% Florida Instititute of Technology, Melbourne, Florida, U.S.A.
%

%error('this function is outdated, and only left here for reference');

n=numel(des);
n=n/2;
des=reshape(des,[2 n]);
a2k=des(2,:)';
a1k=des(1,:)';
kkk=[-1:n-2]';
[alpha]=trigonometric_polynomial_roots(-a1k.*kkk,-a2k.*kkk);
%% evaluate 2nd order derivatives to find out local maximums
if(numel(alpha)<1)% the above trigonometric polynomial is singular, so the feature is directionless
    theta=[];
    return;
end
kkk2=kkk.^2;
talpha=repmat(alpha,[1,n]).*repmat(kkk',[numel(alpha),1]);
sin_k_1_theta=sin(talpha);
cos_k_1_theta=cos(talpha);
a2nd=-cos_k_1_theta.*repmat(a1k(1:end)',[numel(alpha) 1])+sin_k_1_theta.*repmat(a2k(1:end)',[numel(alpha) 1]);
a2nd=a2nd.*repmat(kkk2',[numel(alpha),1]);
a2nd=sum(a2nd,2);
%% the 2nd order derivative has to be negative for an extreme to be maximum
alpha=alpha(a2nd<0);
theta=alpha;
%% calculate the projection of coefficients on each principal direction.
talpha=repmat(alpha,[1,n]).*repmat(kkk',[numel(alpha),1]);
sin_k_1_theta=sin(talpha);
cos_k_1_theta=cos(talpha);
aproj=cos_k_1_theta.*repmat(a1k(1:end)',[numel(alpha) 1])-sin_k_1_theta.*repmat(a2k(1:end)',[numel(alpha) 1]);
aproj=sum(aproj,2);
%% remove principal directions that fell below a certain percentange than
%% the maximum.
mm=max(aproj);
I=aproj>mm*thresh1;
theta=theta(I);
dsig=aproj(I);% directional singular energy


